<template>
  <div class="container">
    <div class="containerTop">
      <!-- 酒店选择 -->
      <div class="topSelect">
        请选择酒店名称：
        <div class="search">
          <el-select
            v-model="tableForm.jdId"
            clearable
            placeholder="请选择酒店"
            class="searchContent"
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.jdName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </div>
        <!-- 入住状态 -->
        <div class="statuSearch">
          请选择居住状态：
          <el-select
            v-model="ruleForm.status"
            clearable
            placeholder="请选择居住状态"
            class="searchContent"
            @change="selectStatus($event)"
          >
            <el-option
              v-for="item in options1"
              :key="item.status"
              :label="item.label"
              :value="item.status"
            >
            </el-option>
          </el-select>
        </div>
        <el-button
          type="primary"
          icon="ios-search"
          label="default"
          class="mr15"
          size="small"
          @click="toSearch"
          >搜索</el-button
        >
        <el-button class="ResetSearch mr10" size="small" @click="resetSearch"
          >重置</el-button
        >
      </div>
      <!-- 按钮 -->
      <div class="buttonArea">
        <el-button class="mr10" size="small" type="primary" @click="goAdd()"
          >添加房间</el-button
        >
      </div>
    </div>
    <div class="center">
      <el-table :data="tableData.tableList" style="width: 100%">
        <el-table-column
          fixed
          prop="jdName"
          label="酒店名称"
          width="120"
          minlength="120"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          fixed
          prop="jdPhone"
          label="酒店联系方式"
          width="120"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          fixed
          prop="fjName"
          label="酒店房间名称"
          width="200"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          fixed
          prop="fjNumber"
          label="酒店房间号"
          width="100"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          fixed
          prop="fjFloor"
          label="房间楼层"
          width="80"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          fixed
          prop="fxName"
          label="房型"
          width="170"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="fjPrice"
          label="房间价格"
          width="80"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="fjPwd"
          label="房间密码"
          width="100"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="fjWifiName"
          label="房间wifi名称"
          width="150"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="fjWifiPwd"
          label="房间wifi密码"
          width="100"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          fixed
          prop="ruzhuren"
          label="入住人"
          width="100"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="status"
          label="房间入住状态"
          width="120"
          header-align="center"
          align="center"
        >
          <template #default="scope">
            <el-switch
              disabled
              v-model="tableData.tableList.status"
              :active-value="scope.row.status === 0"
              :inactive-value="scope.row.status === 1"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="220" align="center">
          <template #default="scope">
            <el-button type="text" size="small" @click="goEdit(scope.row)"
              >编辑</el-button
            >
            <el-button type="text" size="small" @click="del(scope.row)"
              >删除</el-button
            >
            <el-button type="text" size="small" @click="goDetail(scope.row)"
              >详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="pageChange"
        :current-page="tableForm.page"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="tableForm.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total"
        class="blockPage"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  roomListApi,
  groggeryListApi,
  delRoomApi,
} from "@/api/roomInformation";
export default {
  name: "roomIndex",
  created() {
    this.getList(); // 获取所有的酒店and房间
    this.gethotalList(); // 获取所有的酒店
  },
  data() {
    return {
      //首页入住状态
      options1: [
        {
          status: 0,
          label: "未入住",
        },
        {
          status: 1,
          label: "已入住",
        },
      ],
      options: [], //所有酒店名称
      //表单
      ruleForm: {
        fxName: "", //房型名称
        jdName: "", //酒店名称
        fjName: "", //房间名称
        fjNumber: "", //房间号
        fjPrice: "", //房间价格
        fjPwd: "", //房间密码
        fjWifiName: "", //房间wifi名称
        fjWifiPwd: "", //房间wifi密码
        fjImg: "", //房间图片
        jdId: "", //酒店id
        id: 0,
        isDel: 0,
        isShow: 0, //房间是否显示
        sort: 0,
        status: "", //房间入住状态
      },
      //查询所有酒店的房间
      tableForm: {
        limit: 10,
        page: 1,
        jdName: "", //酒店名称
        status: "", //房间入住状态
      },
      // 所有酒店and所有房间
      tableData: {
        tableList: [],
        total: 0,
      },

      //查询所有酒店列表
      hotalForm: {
        limit: 10,
        page: 1,
      },
    };
  },

  methods: {
    //搜索按钮
    async toSearch() {
       this.getList(); // 获取所有的酒店and房间
    },
    // 重置搜索项
    resetSearch() {
      this.tableForm = {
        limit: 10,
        page: 1,
        jdId: "", //酒店 id
        status: "", //房间入住状态
      };
      this.ruleForm.status = "";
    },
    //去添加房间页面
    goAdd() {
      this.$router.push({ name: "roomAdd" });
    },
    //去编辑房间页面
    goEdit(row) {
      // 1.本地缓存
      sessionStorage.setItem("data", JSON.stringify(row.id));
      // 2.跳转传值
      row.id = "" + row.id;
      this.$router.push({ name: "roomEdit", params: { id: row.id } });
    },
    //去详情房间页面
    goDetail(row) {
      // 1.本地缓存
      sessionStorage.setItem("data", JSON.stringify(row.id));
      this.$router.push({ name: "roomDetail", params: { id: row.id } }); //跳转传递 id
    },
    //首页选择居住状态
    async selectStatus(val) {
      this.tableForm.status = val;
    },

    // 获取所有的酒店and房间
    async getList() {
      const res = await roomListApi(this.tableForm);
      this.tableData.tableList = res.list || [];
      this.tableData.total = res.total;
    },

    // 获取所有的酒店
    async gethotalList() {
      const res = await groggeryListApi(this.hotalForm);
      this.options = res.list;
    },

    //提示框按钮
    handleClose(done) {
      done();
    },

    //删除房间
    async del(row) {
      try {
        await this.$confirm("是否删除该房间", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        });
        await delRoomApi({ id: row.id });
        this.$message.success("删除成功");
        this.getList();
      } catch (err) {
        console.log(err);
      }
    },
    //分页
    async pageChange(page) {
      this.tableForm.page = page;
      this.getList();
    },
    //分页
    async handleSizeChange(val) {
      this.tableForm.limit = val;
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  padding: 20px;
  .center {
    margin-top: 30px;
  }
  //顶部搜索部分
  .containerTop {
    width: 100%;
    height: 130px;
    background-color: #fff;
    padding-left: 40px;
    padding-top: 20px;
    .topSelect {
      width: 100%;
      height: 40px;
      display: flex;
      align-items: center;
      font-size: 14px;
      //酒店选择
      .search {
        width: 200px;
        height: 36px;
        border-radius: 8px;
        margin-left: 10px;
      }
      //入住状态
      .statuSearch {
        width: 400px;
        height: 36px;
        // background-color: pink;
        border-radius: 8px;
        display: flex;
        align-items: center;
        margin-left: 30px;
        font-size: 14px;
      }
    }
    .buttonArea {
      width: 300px;
      height: 50px;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  }

  //分页样式
  .block {
    width: 100%;
    height: 80px;
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: end;
  }

  //上传图片样式开始
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    // background-color: blue;
    border: 1px solid #dde0e7;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 98px;
    height: 98px;
    display: block;
  }
  //上传图片样式结束
}
</style>